package com.yt.sys.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.sys.models.entity.SysPermission;
import com.yt.sys.models.service.ISysPermissionService;
import com.yt.sys.server.mapper.SysPermissionMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService(interfaceClass = ISysPermissionService.class)
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {

    /**
     * 根据权限名称查找权限
     * @param permName 权限名称
     * @return 权限信息
     */
    public SysPermission getByPermName(String permName) {
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("perm_name", permName);
        queryWrapper.eq("is_delete", 0);
        return this.getOne(queryWrapper);
    }

    /**
     * 根据URL查找权限
     * @param permUrl 权限URL
     * @return 权限信息
     */
    public SysPermission getByPermUrl(String permUrl) {
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("perm_url", permUrl);
        queryWrapper.eq("is_delete", 0);
        return this.getOne(queryWrapper);
    }

    /**
     * 获取所有未删除的权限
     * @return 权限列表
     */
    public List<SysPermission> getAllActivePermissions() {
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("create_time");
        return this.list(queryWrapper);
    }
}